package com.usian.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.mapper.StockMapper;
import com.usian.pojo.Stock;
import com.usian.service.StockService;
import org.springframework.stereotype.Service;

@Service
public class StockServiceImpl extends ServiceImpl<StockMapper, Stock> implements StockService {


    @Override
    public boolean jian(Integer skuId, Integer num) {

        /**
         * 1:校验参数 非空
         * 2：校验库存够不够  够
         * 3：update  减库存
         */

        boolean b =false;
        if(skuId!=null && num !=null){
            Stock stock = this.lambdaQuery().eq(Stock::getSkuId, skuId).one();
            if(stock!=null){
                if(stock.getNum()>=num){

                    UpdateWrapper<Stock> updateWrapper =new UpdateWrapper<>();
                    updateWrapper.set("num",stock.getNum()-num);
                    updateWrapper.eq("sku_id",skuId);
                    b = this.update(updateWrapper);
                }
            }
        }
        return b;
    }
}
